package cn.koala.key.scraped.controller;

import cn.koala.key.common.excel.ExcelUtils;
import cn.koala.key.scraped.domain.dto.ScrapedOrderDTO;
import cn.koala.key.scraped.domain.dto.ScrapedSerachDTO;
import cn.koala.key.scraped.domain.vo.ScrapedKeyInfoExcel;
import cn.koala.key.scraped.domain.vo.ScrapedOrderDetailVO;
import cn.koala.key.scraped.domain.vo.ScrapedOrderVO;
import cn.koala.key.scraped.service.ScrapedOrderService;
import cn.koala.key.stock.domain.dto.CarVinDTO;
import cn.koala.key.stock.domain.vo.KeyInfoVO;
import cn.koala.key.stock.service.KeyInfoService;
import cn.ehi.authority.annotation.WebAuthentication;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultGenerator;
import cn.seed.common.core.ResultList;
import cn.seed.common.utils.LoggerUtils;
import com.alibaba.excel.support.ExcelTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * 报废管理
 * @author shuai.xu
 * @date 2019/05/27
 */
@RestController
@Api(value = "ScrapedOrder",tags = "报废管理")
public class ScrapedOrderController {

    @Autowired
    private ScrapedOrderService scrapedOrderService;

    @Autowired
    private KeyInfoService keyInfoService;

    @ApiOperation(value = "获取全部报废管理")
    @GetMapping("/scraped/orders")
    public Result<ResultList<ScrapedOrderVO>> list(ScrapedSerachDTO scrapedSerachDTO) {
        return scrapedOrderService.scrapedList(scrapedSerachDTO);
    }

    @ApiOperation(value = "获取报废单详情")
    @GetMapping("/scraped/orders/detail")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00026")
    public Result<ScrapedOrderDetailVO> getScrapedDetailByOrderNo(String orderNo) {
        return scrapedOrderService.getScrapedDetailByOrderNo(orderNo);
    }

    @ApiOperation(value = "报废车辆信息导出")
    @PostMapping("/scraped/print")
    public void exportScrapedKeyInfo(HttpServletResponse response,
                                @RequestBody List<String> keyBarCode){
        Result<List<KeyInfoVO>> list = keyInfoService.scrapedScanCode(keyBarCode);
        List<KeyInfoVO> data = list.getResult();
        List<ScrapedKeyInfoExcel> resultList = scrapedOrderService.dataProcessing(data);
        //导出操作
        try {
            ExcelUtils.setResponse(response,"报废车辆信息");
            ExcelUtils.export(response.getOutputStream(),ExcelTypeEnum.XLSX,resultList,ScrapedKeyInfoExcel.class);
        } catch (Exception e) {
            LoggerUtils.error(ScrapedOrderController.class, e.getMessage());
        }
    }

    @ApiOperation(value = "新增/编辑报废单")
    @PostMapping("/scraped/orders")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00027,KOALAKEY_B_00030")
    public Result insertOrUpdate(@Valid @RequestBody ScrapedOrderDTO scrapedOrderDTO) {
        return scrapedOrderService.insertOrUpdate(scrapedOrderDTO);
    }

    @ApiOperation(value = "提交/作废报废单")
    @PutMapping("/scraped/orders")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00028,KOALAKEY_B_00029")
    public Result invalidOrSubmit(String orderNo,Integer status) {
        return scrapedOrderService.invalidOrSubmit(orderNo,status);
    }
}
